﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.Common;
using BaHuan.DataLayer;
using BaHuan.Entities;
using BaHuan.BusinessLayer;
namespace BaHuan
{
    public partial class FrmBaoCaoQuy : Form
    {
        private ButToanMgt buttoanMgt = new ButToanMgt();
        private TaiKhoanKeToan taikhoanketoan = new TaiKhoanKeToan();
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();
        private NganHangManagement nganhangMgt = new NganHangManagement();
        private ChiNhanhCongTyMgt chinhanhcongtyMgt = new ChiNhanhCongTyMgt();
        private UyNhiemChiManagement uynhiemchiMgt = new UyNhiemChiManagement();
        private PhieuThuTienManagement phieuthutienMgt = new PhieuThuTienManagement();
        private CTTKKeToanLuongTinhManagement cttkketoanluongtinhMgt = new CTTKKeToanLuongTinhManagement();

        public NhanVien nhanvien = new NhanVien();

        public FrmBaoCaoQuy()
        {
            InitializeComponent();
        }        

        private void cboChonTaiKhoan_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cboChonTaiKhoan.SelectedIndex > -1)
            {
                string loaiTaiKhoan = cboChonTaiKhoan.Items[cboChonTaiKhoan.SelectedIndex].ToString();
                switch (loaiTaiKhoan)
                {
                    case "Tiền Việt Nam":
                        cboDoiTuong.DataSource = null;
                        cboDoiTuong.DataSource = GetAllChiNhanhCongTy();
                        cboDoiTuong.DisplayMember = "TenChiNhanh";
                        cboDoiTuong.ValueMember = "Id";
                        break;
                    case "Tiền Việt Nam Gửi Ngân Hàng":
                        cboDoiTuong.DataSource = null;
                        cboDoiTuong.DataSource = GetAllNganHang();
                        cboDoiTuong.DisplayMember = "Ten";
                        cboDoiTuong.ValueMember = "MaSo";
                        break;
                }
            }
        }

        public void FrmBaoCaoQuy_formclosexemreport(bool isReOpen)
        {
            if (isReOpen)
                btnXem_Click(null, null);
        }

        private void cboDoiTuong_Leave(object sender, EventArgs e)
        {
            if (cboChonTaiKhoan.SelectedIndex != 0 )
            {
                if (cboChonTaiKhoan.SelectedIndex == 0)
                {
                    foreach (ChiNhanhCongTy b in cboDoiTuong.Items)
                    {
                        if (b.TenChiNhanh.ToLower().Contains(cboDoiTuong.Text) && cboDoiTuong.Text != "")
                        {
                            cboDoiTuong.SelectedValue = b.Id;
                            break;
                        }
                    }
                }
                else
                {
                    foreach (NganHang b in cboDoiTuong.Items)
                    {
                        if (b.Ten.ToLower().Contains(cboDoiTuong.Text) && cboDoiTuong.Text != "")
                        {
                            cboDoiTuong.SelectedValue = b.Id;
                            break;
                        }
                    }
                }

                if (cboDoiTuong.SelectedValue == null)
                {
                    cboDoiTuong.Text = "";
                }
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            List<BaoCaoQuy> lstbaocaoquy = new List<BaoCaoQuy>();
            List<BaoCaoQuyTongHop> lstbaocaoquytonghop = new List<BaoCaoQuyTongHop>();

            DateTime dtTuNgay = dtpTu.Value;
            DateTime dtDenNgay = dtpDen.Value;

            string sTenDoiTuong = "";
            string sTenTaiKhoan = cboChonTaiKhoan.Text;

            long lDuNoDauKy = 0;
            long lDuCoDauKy = 0;

            ChiNhanhCongTy chinhanhcty = new ChiNhanhCongTy();
            NganHang nganhang = new NganHang();

            if (cboDoiTuong.Text != "")
            {
                sTenDoiTuong = cboDoiTuong.Text;
                if (cboChonTaiKhoan.SelectedIndex == 0)
                    chinhanhcty = (ChiNhanhCongTy)cboDoiTuong.SelectedItem;
                else
                    nganhang = (NganHang)cboDoiTuong.SelectedItem;
            }            

            int iTaiKhoan = cboChonTaiKhoan.SelectedIndex;
            switch (iTaiKhoan)
            {
                case 0:
                    if (chinhanhcty.Id != 0)
                    {
                        lstbaocaoquy = BaoCaoQuyCoDoiTuong111(chinhanhcty, dtTuNgay, dtDenNgay);

                        long lTemp = GetSoDuDauKy("111", chinhanhcty, nganhang, dtTuNgay);
                        if (lTemp >= 0)
                            lDuNoDauKy = lTemp;
                        else
                            lDuCoDauKy = 0 - lTemp;
                    }
                    else
                    {
                        lstbaocaoquytonghop = BaoCaoQuyTongHop111(dtTuNgay, dtDenNgay);
                        foreach (BaoCaoQuyTongHop b in lstbaocaoquytonghop)
                        {
                            ChiNhanhCongTy cn = (from br in GetAllChiNhanhCongTy()
                                                 where br.TenChiNhanh == b.DoiTuong
                                                 select br).FirstOrDefault();

                            long lTemp = GetSoDuDauKy("111", cn, nganhang, dtTuNgay);
                            if (lTemp >= 0)
                                b.NoDau = lTemp;
                            else
                                b.CoDau = 0 - lTemp;

                            lTemp = (lTemp + b.PSNo) - b.PSCo;
                            if (lTemp >= 0)
                                b.NoCuoi = lTemp;
                            else
                                b.CoCuoi = 0 - lTemp;
                        }
                    }

                    break;
                case 1:
                    if (nganhang.Id != 0)
                    {
                        lstbaocaoquy = BaoCaoQuyCoDoiTuong112(nganhang, dtTuNgay, dtDenNgay);

                        long lTemp = GetSoDuDauKy("112", chinhanhcty, nganhang, dtTuNgay);
                        if (lTemp >= 0)
                            lDuNoDauKy = lTemp;
                        else
                            lDuCoDauKy = 0 - lTemp;
                    }
                    else
                    {
                        lstbaocaoquytonghop = BaoCaoQuyTongHop112(dtTuNgay, dtDenNgay);
                        foreach (BaoCaoQuyTongHop b in lstbaocaoquytonghop)
                        {
                            NganHang nh = (from bk in GetAllNganHang()
                                           where bk.Ten == b.DoiTuong
                                           select bk).FirstOrDefault();

                            long lTemp = GetSoDuDauKy("112", chinhanhcty, nh, dtTuNgay);
                            if (lTemp >= 0)
                                b.NoDau = lTemp;
                            else
                                b.CoDau = 0 - lTemp;

                            lTemp = (lTemp + b.PSNo) - b.PSCo;
                            if (lTemp >= 0)
                                b.NoCuoi = lTemp;
                            else
                                b.CoCuoi = 0 - lTemp;
                        }
                    }

                    break;
            }

            if (lstbaocaoquy.ToList().Count > 0)
            {
                FrmXemReport frmXemReport = new FrmXemReport(lstbaocaoquy, sTenDoiTuong, sTenTaiKhoan, dtTuNgay, dtDenNgay, lDuNoDauKy, lDuCoDauKy);
                frmXemReport.formclosexemreport += new FormCloseXemReportEventHandler(FrmBaoCaoQuy_formclosexemreport);
                this.Hide();
                frmXemReport.nhanvien = this.nhanvien;
                frmXemReport.ShowDialog();
                this.Show();
            }
            else if (lstbaocaoquytonghop.ToList().Count > 0)
            {
                FrmXemReport frmXemReport = new FrmXemReport(lstbaocaoquytonghop, sTenDoiTuong, sTenTaiKhoan, dtTuNgay, dtDenNgay);
                this.Hide();
                frmXemReport.ShowDialog();
                this.Show();
            }
            else
            {
                MessageBox.Show("Không có dữ liệu phù hợp");
            }
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = chinhanhcongtyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> lstChinhanhcongty = (List<ChiNhanhCongTy>)resultObj.Result;
            return lstChinhanhcongty;
        }

        public List<UyNhiemChi> GetAllUyNhiemChi()
        {
            ResultObj result = new ResultObj();
            result = uynhiemchiMgt.GetAllUyNhiemChi();
            List<UyNhiemChi> lstUyNhiemChi = (List<UyNhiemChi>)result.Result;
            return lstUyNhiemChi;
        }

        public List<PhieuThuTien> GetAllPhieuThuTien()
        {
            ResultObj result = new ResultObj();
            result = phieuthutienMgt.GetAllPhieuThuTien();
            List<PhieuThuTien> lstPhieuthutien = (List<PhieuThuTien>)result.Result;
            return lstPhieuthutien;
        }

        public List<NganHang> GetAllNganHang()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = nganhangMgt.GetAllNganHang();
            List<NganHang> lstNganHang = (List<NganHang>)resultObj.Result;
            return lstNganHang;
        }

        public List<TaiKhoanKeToan> GetALlTaiKhoanKeToan()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            List<TaiKhoanKeToan> lstTaikhoanketoan = (List<TaiKhoanKeToan>)resultObj.Result;
            return lstTaikhoanketoan;
        }

        public List<CTTKKeToanLuongTinh> GetAllCTTKKeToanLuongTinh()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = cttkketoanluongtinhMgt.GetAllCTTKKeToanLuongTinh();
            List<CTTKKeToanLuongTinh> listCTTKKeToanLuongTinh = (List<CTTKKeToanLuongTinh>)resultObj.Result;

            return listCTTKKeToanLuongTinh;
        }

        public List<ButToan> GetAllButtoan()
        {
            ResultObj result = new ResultObj();
            result = buttoanMgt.GetAllButToan();
            List<ButToan> lstButtoan = (List<ButToan>)result.Result;
            return lstButtoan;
        }

        public long GetSoDuDauKy(string sMaSoTK, ChiNhanhCongTy chinhanhcty, NganHang nganhang, DateTime dtTuNgay)
        {
            long lTemp = 0;
            List<BaoCaoQuy> lstbaocaoquytemp = new List<BaoCaoQuy>();

            var tk = (from a in GetAllCTTKKeToanLuongTinh()                      
                      select a);

            if (sMaSoTK == "111")
            {
                CTTKKeToanLuongTinh tklt = tk.Where(a => a.TaiKhoan == "111" && a.DoiTuong == chinhanhcty.Id.ToString()).FirstOrDefault();
                if (tklt != null)
                    lTemp = tklt.SoDuBanDau;

                lstbaocaoquytemp = BaoCaoQuyCoDoiTuong111(chinhanhcty, dtTuNgay);
            }
            else if (sMaSoTK == "112")
            {
                CTTKKeToanLuongTinh tklt = tk.Where(a => a.TaiKhoan == "112" && a.DoiTuong == nganhang.Id.ToString()).FirstOrDefault();
                if (tklt != null)
                    lTemp = tklt.SoDuBanDau;

                lstbaocaoquytemp = BaoCaoQuyCoDoiTuong112(nganhang, dtTuNgay);
            }            

            foreach (BaoCaoQuy b in lstbaocaoquytemp)
            {
                lTemp = (lTemp + b.GhiNo) - b.GhiCo;
            }
            
            return lTemp;
        }

        public List<BaoCaoQuyTongHop> BaoCaoQuyTongHop111(DateTime dtTuNgay, DateTime dtDenNgay)
        {
            List<BaoCaoQuyTongHop> lst = new List<BaoCaoQuyTongHop>();
            
            ResultObj resultObj = new ResultObj();
            resultObj = buttoanMgt.GetButToanForCongNoTongHop111(dtTuNgay, dtDenNgay);
            List<ButToan> lstButToan = (List<ButToan>)resultObj.Result;

            if (lstButToan.Count > 0)
            {
                foreach (ButToan bt in lstButToan)
                {
                    BaoCaoQuyTongHop bc = new BaoCaoQuyTongHop();
                    bc.DoiTuong = bt.ChiNhanh;
                    bc.NoDau = 0;
                    bc.CoDau = 0;
                    bc.PSNo = bt.GhiNo;
                    bc.PSCo = bt.GhiCo;
                    bc.NoCuoi = 0;
                    bc.CoCuoi = 0;
                    lst.Add(bc);
                }
            }

            return lst;
        }

        public List<BaoCaoQuyTongHop> BaoCaoQuyTongHop112(DateTime dtTuNgay, DateTime dtDenNgay)
        {
            List<BaoCaoQuyTongHop> lst = new List<BaoCaoQuyTongHop>();

            ResultObj resultObj = new ResultObj();
            resultObj = buttoanMgt.GetButToanForCongNoTongHop112(dtTuNgay, dtDenNgay);
            List<ButToan> lstButToan = (List<ButToan>)resultObj.Result;

            if (lstButToan.Count > 0)
            {
                foreach (ButToan bt in lstButToan)
                {
                    BaoCaoQuyTongHop bc = new BaoCaoQuyTongHop();
                    bc.DoiTuong = bt.ChiNhanh; // Temp field for NganHang
                    bc.NoDau = 0;
                    bc.CoDau = 0;
                    bc.PSNo = bt.GhiNo;
                    bc.PSCo = bt.GhiCo;
                    bc.NoCuoi = 0;
                    bc.CoCuoi = 0;
                    lst.Add(bc);
                }
            }

            return lst;
        }

        public List<BaoCaoQuy> BaoCaoQuyCoDoiTuong111(ChiNhanhCongTy chinhanhcty, DateTime dtTuNgay, DateTime dtDenNgay)
        {
            List<ButToan> lstButToan = (from bt in GetAllButtoan()
                                        where bt.NgayThang.Date >= dtTuNgay.Date && bt.NgayThang.Date <= dtDenNgay.Date
                                              && bt.TKGoc.StartsWith("111") && bt.ChiNhanh == chinhanhcty.TenChiNhanh
                                        select bt).ToList();

            return AddItemToListCoDoiTuong(lstButToan);
        }

        public List<BaoCaoQuy> BaoCaoQuyCoDoiTuong111(ChiNhanhCongTy chinhanhcty, DateTime dtTuNgay)
        {
            List<ButToan> lstButToan = (from bt in GetAllButtoan()
                                        where bt.NgayThang.Date < dtTuNgay.Date && bt.TKGoc.StartsWith("111") && bt.ChiNhanh == chinhanhcty.TenChiNhanh
                                        select bt).ToList();

            return AddItemToListCoDoiTuong(lstButToan);
        }

        public List<BaoCaoQuy> BaoCaoQuyCoDoiTuong112(NganHang nganhang, DateTime dtTuNgay, DateTime dtDenNgay)
        {
            List<ButToan> lstButToan = (from c in GetAllButtoan()
                                        join unc in GetAllUyNhiemChi() on c.SoChungTu equals unc.MaSo
                                        where c.NgayThang.Date >= dtpTu.Value.Date && c.NgayThang.Date <= dtpDen.Value.Date 
                                            && c.TKGoc.StartsWith("112") && unc.IdNganHang == nganhang.Id
                                        select c).ToList();

            return AddItemToListCoDoiTuong(lstButToan);
        }

        public List<BaoCaoQuy> BaoCaoQuyCoDoiTuong112(NganHang nganhang, DateTime dtTuNgay)
        {
            List<ButToan> lstButToan = (from c in GetAllButtoan()
                                        join unc in GetAllUyNhiemChi() on c.SoChungTu equals unc.MaSo
                                        where c.NgayThang.Date < dtpTu.Value.Date && c.TKGoc.StartsWith("112") && unc.IdNganHang == nganhang.Id
                                        select c).ToList();

            return AddItemToListCoDoiTuong(lstButToan);
        }

        public List<BaoCaoQuy> AddItemToListCoDoiTuong(List<ButToan> lstButToan)
        {
            List<BaoCaoQuy> lst = new List<BaoCaoQuy>();

            if (lstButToan.ToList().Count > 0)
            {
                foreach (var bt in lstButToan)
                {
                    BaoCaoQuy bc = new BaoCaoQuy();
                    bc.NgayThang = bt.NgayThang;
                    bc.DienGiai = bt.DienGiai;
                    bc.GhiNo = bt.GhiNo;
                    bc.GhiCo = bt.GhiCo;
                    bc.TKDoiUng = bt.TKDoiUng;
                    if (bt.SoChungTu.StartsWith("PT"))
                    {
                        bc.PhieuChi = null;
                        bc.PhieuThu = bt.SoChungTu;
                    }
                    else if (bt.SoChungTu.StartsWith("PC"))
                    {
                        bc.PhieuThu = null;
                        bc.PhieuChi = bt.SoChungTu;
                    }
                    lst.Add(bc);
                }
            }

            return lst;
        }       
    }
}
